function [ branchpt, imageStructure ] = breakGraphAtBranches( imSkel )
%UNTITLED2 Summary of this function goes here
%   Returns two structures, one that contains an image for each part of the
%   graph returned, and one that returns information about the
%   branchpoints, including the branchpoint and its neighborhood.  The
%   neighborhood of the branchpoint is not included in each of the images.

    %Get a list of of branchpoints and their neighbors for each branch
    branchpoints = bwmorph(imSkel, 'branchpoints');
    [row ,col]  = find(branchpoints);
    branchpoint = [row,col];
    numBranchpoints = size(branchpoint,1);
    branchpt = struct('Neighbors',[],'Branchpoint',[]);
    imSkelTemp = imSkel;
    for k = 1:numBranchpoints
        
        indexBranchpoints = sub2ind(size(imSkel),branchpoint(1,:), branchpoint(2,:));
        neighRows = [branchpoint(k,1) - 1:branchpoint(k,1) +1];
        neighCols = [ branchpoint(k,2) -1 : branchpoint(k,2)+1];
        neighborhood = imSkel(neighRows,neighCols);
        [row,col] = find(neighborhood);
        displacement = [row-2,col-2];
        numNeighbors = size(displacement,1);
        neighbors =  displacement+repmat(branchpoint(k,:),[numNeighbors 1]);
        neighIndices = sub2ind(size(imSkel),neighbors(:,1), neighbors(:,2) );
        branchpt(k).Neighbors =  neighbors;
        branchpt(k).BranchPoint = branchpoint(k,:);
        
        imSkelTemp(neighIndices) = 0;
        imSkelTemp(indexBranchpoints)= 0; 
        
    end
    
    props = regionprops(imSkelTemp,'PixelIdxList');
    
    imageStructure = struct('Image',[]);
    for k = 1:length(props)
        indices = props(k).PixelIdxList;
        tempImage = zeros(size(imSkel));
        tempImage(indices) = 1;
        imageStructure(k).Image = tempImage;
    end
        
end
        
    
  
    